package com.ziyuan.service.impl;

import com.ziyuan.mapper.CategoryMapper;
import com.ziyuan.pojo.Category;
import com.ziyuan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryService implements com.ziyuan.service.CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public void add(Category category) {
        Map<String,String> o = ThreadLocalUtil.get();
        category.setCreateUser(Integer.parseInt(o.get("id")));
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.inster(category);
    }

    @Override
    public List<Category> get() {
        Map<String,String> o = ThreadLocalUtil.get();
        Integer id = Integer.parseInt(o.get("id"));

        return categoryMapper.get(id);
    }

    @Override
    public Category findById(Integer id) {
        Category category = categoryMapper.findById(id);
        return category;
    }

    @Override
    public void PutCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }

    @Override
    public void delete(Integer id) {
        categoryMapper.delete(id);
    }
}
